'''
# -*- coding: utf-8 -*-
@author: saiksy
'''

import os
import json
import pycurl
import time
import urllib2
import sys

try:
    # cStringIO is faster
    from cStringIO import StringIO
except ImportError:
    # otherwise this'll do
    from StringIO import StringIO

class stock_trade_info:
    def __init__(self, info, code):
        self.stock_info = self.parse_(info, code)
        

    def parse_(self, info, code):
        stock_info = {}
        stock_info["name"] = info[0].decode('gb2312')
        stock_info["code"] = code
        stock_info["date"] = info[30]
        stock_info["time"] = info[31]
        stock_info["OpenningPrice"] = float(info[1])
        stock_info["closingPrice"] = float(info[2])
        stock_info["currentPrice"] = float(info[3])
        stock_info["hPrice"] = float(info[4])
        stock_info["lPrice"] = float(info[5])
        stock_info["competitivePrice"] = float(info[6])
        stock_info["auctionPrice"] = float(info[7])
        stock_info["totalNumber"] = float(info[8])
        stock_info["turnover"] = float(info[9])
        stock_info["buyOne"] = float(info[10])
        stock_info["buyOnePrice"] = float(info[11])
        stock_info["buyTwo"] = float(info[12])
        stock_info["buyTwoPrice"] = float(info[13])
        stock_info["buyThree"] = float(info[14])
        stock_info["buyThreePrice"] = float(info[15])
        stock_info["buyFour"] = float(info[16])
        stock_info["buyFourPrice"] = float(info[17])
        stock_info["buyFive"] = float(info[18])
        stock_info["buyFivePrice"] = float(info[19])
        stock_info["sellOne"] = float(info[20])
        stock_info["sellOnePrice"] = float(info[21])
        stock_info["sellTwo"] = float(info[22])
        stock_info["sellTwoPrice"] = float(info[23])
        stock_info["sellThree"] = float(info[24])
        stock_info["sellThreePrice"] = float(info[25])
        stock_info["sellFour"] = float(info[26])
        stock_info["sellFourPrice"] = float(info[27])
        stock_info["sellFive"] = float(info[28])
        stock_info["sellFivePrice"] = float(info[29])
        return stock_info
    def get_stock_info(self):
        return self.stock_info
         
class maket_info:
    def __init__(self, info):
        self.name = info[0]
        self.curdot = float(info[1])
        self.curprice = float(info[1])
        self.rate = float(info[1])
        self.dealnumber = float(info[1])
        self.turnover = float(info[1])
                 

class StockMonitor:
    def __init__(self, stock_code_list):
        self.url = 'http://hq.sinajs.cn/list=' + ','.join(stock_code_list)
        self.stocks = stock_code_list
        
        
    def __del__(self):
        pass
    
    def parse(self, data):
        index = 0
        for line in data.split(';'):
            #print line
            line = line.strip()
            stock = line.split('=')
            if len(stock) < 2:
                continue
            stock = stock[1].strip('"')
            info = stock.split(',')
            if len(info) <= 0:
                continue 
            
            stockInfo = stock_trade_info(info, self.stocks[index]).get_stock_info()
            print("%s %s" % (stockInfo['name'], stockInfo['code']))
            index = index + 1
            
    def monitoring(self):
        while(True):
            os.system("cls")
            print('-' * 10 + 'My Stock Monitor System' + '-' * 10)
            req = urllib2.Request(self.url)
            response = urllib2.urlopen(req)
            data = response.read().strip()
            #data = data.decode('gb2312')
            self.parse(data)
            time.sleep(3)
        
if __name__ == '__main__':
    stockMonitor = StockMonitor(['sh600795', 'sh601669'])
    stockMonitor.monitoring()
    
    